package com.shendu.task.bo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.shendu.task.domain.UserDoc;

@Component
public class UserBo {

	@Autowired
	private MongoTemplate mongo;

	public void save(UserDoc user) {
		mongo.save(user);

	}

	public UserDoc getByName(String name) {
		Criteria criteria = new Criteria();
		criteria.and("name").is(name);
		Query query = new Query(criteria);
		return mongo.findOne(query, UserDoc.class);
	}

	public List<UserDoc> list(UserDoc user) {
		Criteria criteria = new Criteria();
		if(!StringUtils.isEmpty(user.getDepartNo())) {
			criteria.and("departNo").is(user.getDepartNo());
		}
		Query query = new Query(criteria);
		return mongo.find(query, UserDoc.class);
	}

}
